#-*- coding: utf-8 -*-

import sqlite3 as lite
import sys
import ConfigParser
class Client():

    def __init__(self):
        #config = ConfigParser.RawConfigParser()
        #config.read('config.cfg')
        #self.db = config.get('db', 'name')   
        self.db = "database.db"

    def add(self,//sqlValues//):
        try:            
            con = lite.connect(self.db)
            cur = con.cursor()                
            cur.execute("//sqlCreate//",(//sqlValues//))
            con.commit()              
        except lite.Error, e:
            
            if con:
                con.rollback()
                
            print "Error %s:" % e.args[0]
            sys.exit(1)
            
        finally:
            
            if con:
                con.close()  
    def edit(self,id,//sqlValues//):
        try:            
            con = lite.connect(self.db)
            cur = con.cursor()    
            cur.execute("//sqlUpdate//",(//sqlValues//,id))
            con.commit()              
        except lite.Error, e:
            
            if con:
                con.rollback()
                
            print "Error %s:" % e.args[0]
            sys.exit(1)
            
        finally:
            
            if con:
                con.close() 

    def delete_id(self,id):
        try:            
            con = lite.connect(self.db)
            cur = con.cursor()    
            cur.execute("//sqlDelete//",(id,))
            con.commit()              
        except lite.Error, e:
            
            if con:
                con.rollback()
                
            print "Error %s:" % e.args[0]
            sys.exit(1)
            
        finally:
            
            if con:
                con.close()  

    def find_all(self):
        try:            
            con = lite.connect(self.db)
            cur = con.cursor()    
            cur.execute("//sqlSelect//")
            rows = cur.fetchall()
            return rows
        except lite.Error, e:
            
            if con:
                con.rollback()
                
            print "Error %s:" % e.args[0]
            sys.exit(1)
            
        finally:
            
            if con:
                con.close()  

    def find_by_id(self,id):
        try:            
            con = lite.connect(self.db)
            cur = con.cursor()    
            cur.execute("//sqlRead//",(id,))
            rows = cur.fetchall()
            return rows
        except lite.Error, e:
            
            if con:
                con.rollback()
                
            print "Error %s:" % e.args[0]
            sys.exit(1)
            
        finally:
            
            if con:
                con.close() 
